/*
 * Copyright (C) 2009 Yury Kudryashov.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package name.kudryashov.base;

import name.kudryashov.annotations.Immutable;

import java.util.NoSuchElementException;

/**
 * Some object representing a value in {@link Option}.
 * @param <T> type of the value
 */
@Immutable
final class Some<T> extends Option<T> {

    private final T x;

    private Some(T x) {
        this.x = x;
    }

    public boolean isEmpty() {
        return false;
    }

    public T get() throws NoSuchElementException {
        return x;
    }

    @Override
    public String toString() {
        return "Some(" + x + ")";
    }

    /**
     * Constructs a new {@link Some} with the given value
     * @param x value
     * @param <T> type of value
     * @return new {@link Some} value
     */
    public static <T> Some<T> some(T x) {
        return new Some<T>(x);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Some some = (Some) o;

        return !(x != null ? !x.equals(some.x) : some.x != null);
    }

    @Override
    public int hashCode() {
        return x != null ? x.hashCode() : 0;
    }

    private static final long serialVersionUID = 4704425834176389829L;
}